//
// Created by Administrator on 2020/7/2.
//

#ifndef AVPLAYER_BASECHANNEL_H
#define AVPLAYER_BASECHANNEL_H

#include "safe_queue.h"

extern "C" {
#include <libavcodec/avcodec.h>
};

/**
 * 父类的析构函数必须是虚函数，否则内存释放有问题
 */
class BaseChannel {
public:
    int streamId;
    AVCodecContext *avCodecContext;
    bool isPlaying = 0;

    SafeQueue<AVPacket *> packets;
    SafeQueue<AVFrame *> frames;

    BaseChannel(int id, AVCodecContext *codecContext) : streamId(id), avCodecContext(codecContext) {
        packets.setReleaseCallback(releaseAVPacket);
        frames.setReleaseCallback(releaseAVFrame);
    }

    virtual ~BaseChannel() {
        packets.clear();
        frames.clear();
    }

    /**
 * 释放 AVPacket
 * @param packet
 */
    static void releaseAVPacket(AVPacket **packet) {
        if (packet) {
            av_packet_free(packet);
            *packet = 0;
        }
    }

    /**
     * 释放 AVFrame
     * @param frame
     */
    static void releaseAVFrame(AVFrame **frame) {
        if (frame) {
            av_frame_free(frame);
            *frame = 0;
        }
    }


    //纯虚函数（抽象方法）
    virtual void start() = 0;

    virtual void stop() = 0;
};


#endif //AVPLAYER_BASECHANNEL_H
